//
//  FurnitureCollection.m
//  FKGW
//
//  Created by song ce on 2017/2/24.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "FurnitureCollection.h"
#import "BaseNavigationViewController.h"
#import "UIView+Extend.h"
#define KColumCount 3
#import "FunitureShopViewController.h"
@implementation FurnitureCollection

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self _initSubviews];
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return self;
}

-(void)setDataSoure:(NSMutableArray *)dataSoure{
    _dataSoure = dataSoure;
    [_myCollectionView reloadData];
}
- (void)_initSubviews{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //设置每个item的大小
//    layout.itemSize = CGSizeMake(50,50);
    //每一行之间的最小距离
    layout.minimumLineSpacing = 10;
    //同一行item之间的最小间隙
    layout.minimumInteritemSpacing =10;
    //设置滑动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    UICollectionView *collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:layout];
    collectionV.showsVerticalScrollIndicator = NO;
    //设置分页
//    collectionV.pagingEnabled = YES;
    [self addSubview:collectionV];
    collectionV.backgroundColor = [UIColor whiteColor];
    //设置代理
    collectionV.delegate = self;
    collectionV.dataSource = self;
    //注册
    [collectionV registerNib:[UINib nibWithNibName:@"FurnitureCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"FurnitureCollectionCell"];
    
    //注册头视图
//    [collectionV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [collectionV registerNib:[UINib nibWithNibName:@"CollectionHeadV" bundle:nil]  forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeadV"];
    
    _myCollectionView = collectionV;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;{
    return self.dataSoure.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;{
    MoreGroup *group = self.dataSoure[section];
    return group.contents.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;{
    FurnitureCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FurnitureCollectionCell" forIndexPath:indexPath];
    MoreGroup *group = self.dataSoure[indexPath.section];
    NSArray *contents = group.contents;
    cell.model = contents[indexPath.row];
    return cell;
}

//创建头视图或者尾视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    CollectionHeadV *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeadV" forIndexPath:indexPath];
   MoreGroup *group = self.dataSoure[indexPath.section];
    view.group = group;
    return view;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
{
    return CGSizeMake(Main_Screen_Width, 30);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat with = (Main_Screen_Width-2*10-2*18)/3;
    CGFloat height = with*1.198;
    return CGSizeMake(with, height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {

    UIEdgeInsets edge = UIEdgeInsetsMake(5, 18, 0 ,18);

    return edge;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MoreGroup *group = self.dataSoure[indexPath.section];
    NSArray *contents = group.contents;
    FurnitureModel *model = contents[indexPath.row];
    FunitureShopViewController *fs = [[FunitureShopViewController alloc]init];
    fs.vcName =model.name;
    fs.productTypeId = model.id;
    [[NSNotificationCenter defaultCenter]postNotificationName:@"productTypeId" object:model.id];
    [[self secondNavigationController] pushViewController:fs animated:YES];
}

- (BaseNavigationViewController*)secondNavigationController{
    UIResponder *next = self.nextResponder;
    while (next!=nil) {
        if([next isKindOfClass:[BaseNavigationViewController class]]){
            return (BaseNavigationViewController*)next;
        }
        next = next.nextResponder;
    }
    return nil;
}

@end
